package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.platform.dto.BasicDeviceOperationRecordDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.mapper.BasicDeviceOperationRecordMapper;
import com.iwomy.secureplat.platform.service.BasicDeviceModelService;
import com.iwomy.secureplat.platform.service.BasicDeviceOperationRecordService;
import com.iwomy.secureplat.platform.service.BasicDeviceTypeService;
import com.iwomy.secureplat.platform.vo.BasicDeviceOperationRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备操作记录表
 *
 * @author pig
 * @date 2023-11-22 17:59:04
 */
@Service
public class BasicDeviceOperationRecordServiceImpl extends ServiceImpl<BasicDeviceOperationRecordMapper, BasicDeviceOperationRecordEntity> implements BasicDeviceOperationRecordService {

	@Autowired
	private BasicDeviceTypeService basicDeviceTypeService;

	@Autowired
	private BasicDeviceModelService basicDeviceModelService;

	@Override
	public Page<BasicDeviceOperationRecordVO> getBasicDeviceOperationRecordPage(Page page, BasicDeviceOperationRecordDTO basicDeviceOperationRecordDTO) {
		//多表查询
		MPJLambdaWrapper<BasicDeviceOperationRecordEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.selectAll(BasicDeviceOperationRecordEntity.class)
				.selectAs(BasicDeviceEntity::getImei, BasicDeviceOperationRecordVO::getDeviceImei)
				.selectAs(BasicDeviceEntity::getDeviceTypeId, BasicDeviceOperationRecordVO::getDeviceTypeid)
				.selectAs(BasicDeviceEntity::getDeviceModelId, BasicDeviceOperationRecordVO::getDeviceModelid)

				.leftJoin(BasicDeviceEntity.class, BasicDeviceEntity::getId, BasicDeviceOperationRecordEntity::getDeviceId);

		if (ObjectUtil.isNotEmpty(basicDeviceOperationRecordDTO.getType())) {
			wrapper.like(BasicDeviceOperationRecordEntity::getType, basicDeviceOperationRecordDTO.getType());
		}
		if (ArrayUtil.isNotEmpty(basicDeviceOperationRecordDTO.getCreateTime())) {
			wrapper.ge(BasicDeviceOperationRecordEntity::getCreateTime, basicDeviceOperationRecordDTO.getCreateTime()[0]).le(BasicDeviceOperationRecordEntity::getCreateTime,
					basicDeviceOperationRecordDTO.getCreateTime()[1]);
		}

		Page<BasicDeviceOperationRecordVO> basicDeviceOperationRecordPage = this.getBaseMapper().selectJoinPage(page, BasicDeviceOperationRecordVO.class, wrapper);
		List<BasicDeviceOperationRecordVO> records = basicDeviceOperationRecordPage.getRecords();

		for (BasicDeviceOperationRecordVO record : records) {

			Long deviceTypeId = record.getDeviceTypeid();

			BasicDeviceTypeEntity basicDeviceType = basicDeviceTypeService.lambdaQuery()
					.eq(BasicDeviceTypeEntity::getId, deviceTypeId)
					.one();
			if (basicDeviceType != null) {
				String deviceTypeName = basicDeviceType.getTypeName();
				record.setDeviceTypeName(deviceTypeName);
			}

			Long deviceModelId = record.getDeviceModelid();
			BasicDeviceModelEntity basicDeviceModel = basicDeviceModelService.lambdaQuery()
					.eq(BasicDeviceModelEntity::getId, deviceModelId)
					.one();
			if (basicDeviceModel != null) {
				String deviceModelName = basicDeviceModel.getDeviceModelName();
				record.setDeviceModelName(deviceModelName);
			}
		}
		return basicDeviceOperationRecordPage;

	}
}